﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ServiceStack.Configuration;

namespace ServiceStack.ContainerAdapter.Unity.Tests
{
    [TestClass]
    public class UnityContainerAdapterTest
    {
        private IUnityContainer _unityContainer;
        private IContainerAdapter _unityContainerAdapter;

        [TestInitialize]
        public void SetUp()
        {
            InitContainer();
        }

        private void InitContainer()
        {
            _unityContainer = new UnityContainer();
            _unityContainerAdapter = new UnityContainerAdapter(_unityContainer);
        }

        [TestMethod]
        public void ResolveReturnsRegisteredInstances()
        {
            //ARRANGE 
            _unityContainer.RegisterType<Person>();
            _unityContainer.RegisterType<Student>();
            
            //ACT
            var person = _unityContainerAdapter.Resolve<Person>();
            var student = _unityContainerAdapter.Resolve<Student>();

            //ASSERT
            Assert.IsTrue(person != null);
            Assert.IsTrue(student != null);            
        }

        [TestMethod]
        public void TryResolveReturnsRegisteredInstances()
        {
            //ARRANGE 
            _unityContainer.RegisterType<Person>();
            _unityContainer.RegisterType<Student>();

            //ACT
            var person = _unityContainerAdapter.TryResolve<Person>();
            var student = _unityContainerAdapter.TryResolve<Student>();

            //ASSERT
            Assert.IsTrue(person != null);
            Assert.IsTrue(student != null);
        }

        [TestMethod]
        [ExpectedException(typeof(ResolutionFailedException))]
        public void ResolveReturnsErrorForNotRegisteredClass()
        {
            //ACT
            _unityContainerAdapter.Resolve<IRepository>();            
        }

        [TestMethod]        
        public void TryResolveReturnsEmptyObjectForNotRegisteredClass()
        {
            //ACT
            var repository = _unityContainerAdapter.TryResolve<IRepository>();

            Assert.IsTrue(repository == null);
        }
    }


    public class Person
    {
        public string Name { get; set; }
    }
    public class Student: Person
    {
        public string SchoolName { get; set; }
    }

    public interface IRepository
    {
        
    }
    public class Repository:IRepository
    {
        
    }
}
